package com.liuhengjia.service.impl;

import com.liuhengjia.mapper.UserMapper;
import com.liuhengjia.pojo.User;
import com.liuhengjia.service.UserService;
import com.liuhengjia.util.Md5s;
import com.liuhengjia.util.ThreadLocals;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public void register(String username, String password) {
        // 密码加密
        password = Md5s.getMD5String(password);
        // 添加
        userMapper.insertOne(username, password);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        Map<String, Object> decodeMap = ThreadLocals.get();
        userMapper.updateAvatar(avatarUrl, (Integer) decodeMap.get("id"));
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String, Object> decodeMap = ThreadLocals.get();
        userMapper.updatePwd(Md5s.getMD5String(newPwd), (Integer) decodeMap.get("id"));
    }
}
